package com.lynx.core;
/**
 * 
 */
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.lynx.core.type.Encoding;

@SuppressWarnings("unchecked")
public class ApiResult {
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	
	public <T> T getParam(String key,Class<T> t) {
		T result = null;
		String name = t.getName();
		String val = request.getParameter(key);
		if(!StringUtils.equals(String.class.getName(),name)&&StringUtils.isBlank(val)){
			val = "0";
		}
		if (StringUtils.equals(name, Integer.class.getName())||StringUtils.equals(name,"int")) {
			result = (T)Integer.valueOf(val);
		} else if (StringUtils.equals(name, Long.class.getName())||StringUtils.equals(name,"long")) {
			result = (T) Long.valueOf(val);
		} else if (StringUtils.equals(name, Float.class.getName())||StringUtils.equals(name,"float")) {
			result = (T) Float.valueOf(val);
		} else if (StringUtils.equals(name, Double.class.getName())||StringUtils.equals(name,"double")) {
			result = (T) Double.valueOf(val);
		} else if (StringUtils.equals(name, Byte.class.getName())||StringUtils.equals(name,"byte")) {
			result = (T) Byte.valueOf(val);
		} else if (StringUtils.equals(name, Short.class.getName())||StringUtils.equals(name,"short")) {
			result = (T) Short.valueOf(val);
		} else {
			result = (T) val;
		}
		return result;
	}

	public String[] getParams(String key) {
		return request.getParameterValues(key);
	}
	
	public String[] getParams(String key,Encoding encoding) {
		String[] tmps = null;
		String[] vals = request.getParameterValues(key);
		if(vals!=null&&vals.length>0){
			tmps=new String[vals.length];
			for(int i=0;i<vals.length;i++){
				try {
					tmps[i] =  new String(vals[i].getBytes(Encoding.iso_8859_1.getCode()),encoding.getCode());
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
		}
		return tmps;
	}
	
	public String[] getParams(String key,Encoding sourceencoding,Encoding destencoding) {
		String[] tmps = null;
		String[] vals = request.getParameterValues(key);
		if(vals!=null&&vals.length>0){
			tmps=new String[vals.length];
			for(int i=0;i<vals.length;i++){
				try {
					tmps[i] =  new String(vals[i].getBytes(sourceencoding.getCode()),destencoding.getCode());
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
		}
		return tmps;
	}

	public void setRequestValue(String key, Object val) {
		request.setAttribute(key, val);
	}

	public <T> T getRequestValue(String key,Class<T> t) {
		return (T) request.getAttribute(key);
	}

	public Object getRequestValue(String key) {
		return request.getAttribute(key);
	}

	public void setSessionValue(String key, Object val) {
		request.getSession().setAttribute(key, val);
	}

	public <T> T getSessionValue(String key, Class<T> val) {
		return (T) request.getSession().getAttribute(key);
	}

	public Object getSessionValue(String key) {
		return request.getSession().getAttribute(key);
	}

	public void setEncoding(Encoding encoding) throws Exception {
		request.setCharacterEncoding(encoding.getCode());
		response.setCharacterEncoding(encoding.getCode());
	}

	public void write(String val, Encoding encoding) throws Exception {
		response.setCharacterEncoding(encoding.getCode());
		response.getWriter().print(val);
	}

	public void write(String val) throws Exception {
		response.getWriter().print(val);
	}

	public void writeUTF_8(String val) throws Exception {
		response.setCharacterEncoding(Encoding.utf_8.getCode());
		response.getWriter().print(val);
	}

	private Object model;

	public Object getModel() {
		return model;
	}

	public void setModel(Object model) {
		this.model = model;
	}

	public HttpServletRequest getRequest() {
		return this.request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return this.response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}
}